//
//  NYUserFormSMSCell.m
//  NiuWaJiaoYu
//
//  Created by MinLison on 2017/11/3.
//  Copyright © 2017年 niuwaketang. All rights reserved.
//

#import "NYUserFormSMSCell.h"
#import "NYSMSValidator.h"
#import "MZTimerLabel.h"
#import "NYPhoneCondition.h"
#import "NYPhoneValidator.h"
@interface NYUserFormSMSCell ()
@property(nonatomic, strong) QMUIButton *rightView;
@property(nonatomic, strong) MZTimerLabel *timerLabel;
@property(nonatomic, strong) UIView *verticalLineView;
@end

@implementation NYUserFormSMSCell

- (void)setUp
{
        [super setUp];
        @weakify(self);
        [self.rightView jk_addActionHandler:^(NSInteger tag) {
                @strongify(self);
                if (self.field.action)
                {
                        self.field.action(self);
                        [self configTimerLabel:YES];
                }
        }];

        self.rightView.enabled = [[NYPhoneCondition condition] check:NYUserManager.mobile];
        self.verticalLineView = [[UIView alloc] init];
        self.verticalLineView.backgroundColor = UIColorHex(0xE6E6E6);
        
        [self.contentCoverView addSubview:self.verticalLineView];
        [self.contentCoverView addSubview:self.timerLabel];
        [self.contentCoverView addSubview:self.rightView];
        self.textField.validator = [NYSMSValidator validator];
        
        [self.KVOController observe:NYUserManager keyPath:@keypath(NYUserManager,mobile) options:(NSKeyValueObservingOptionNew) block:^(id  _Nullable observer, id  _Nonnull object, NSDictionary<NSString *,id> * _Nonnull change) {
                self.rightView.enabled = [[NYPhoneCondition condition] check:NYUserManager.mobile];
        }];
}
- (void)update
{
        [super update];
        [self.leftView setImage:[UIImage login_yan_zheng_ma] forState:(UIControlStateNormal)];
        self.textField.keyboardType = UIKeyboardTypeNumberPad;
}


- (void)layout
{
        [self configTimerLabel:NO];
        [self.contentCoverView mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.edges.equalTo(self.contentView).valueOffset([NSValue valueWithUIEdgeInsets:UIEdgeInsetsMake(__WGHeight(7.5), __WGWidth(40), __WGHeight(7.5), __WGWidth(40))]);
        }];
        [self.leftView mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(self.contentCoverView).offset(__WGWidth(UserFormLeftMargin));
                make.size.mas_equalTo(CGSizeMake(__WGWidth(UserFormleftViewSize.width), __WGHeight(UserFormleftViewSize.height)));
                make.centerY.equalTo(self.contentCoverView);
        }];
        
        [self.textField mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(self.leftView.mas_right).offset(__WGWidth(UserFormTextFieldInsets.left));
                make.right.equalTo(self.rightView.mas_left).offset(-__WGWidth(UserFormRightMargin));
                make.centerY.equalTo(self.leftView);
                make.top.greaterThanOrEqualTo(self.contentCoverView).offset(__WGWidth(UserFormTextFieldInsets.top));
                make.bottom.lessThanOrEqualTo(self.contentCoverView).offset(-__WGWidth(UserFormTextFieldInsets.bottom));
        }];
        
        
        [self.rightView mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(self.leftView);
                make.right.equalTo(self.contentCoverView.mas_right).offset(-__WGWidth(UserFormRightMargin));
                make.width.mas_equalTo(__WGWidth(80));
                make.top.bottom.equalTo(self.contentCoverView);
        }];
        
        [self.timerLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(self.leftView);
                make.left.equalTo(self.textField.mas_right);
                make.right.equalTo(self.contentCoverView.mas_right).offset(-__WGWidth(UserFormRightMargin));
        }];
        [self.lineView mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(self.leftView.imageView.mas_left);
                make.right.equalTo(self.rightView.mas_right);
                make.bottom.equalTo(self.contentCoverView);
                make.height.mas_equalTo(__WGWidth(UserFormBootomLineHeight));
        }];
        [self.verticalLineView mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.height.mas_equalTo(__WGHeight(18));
                make.width.mas_equalTo(__WGWidth(1));
                make.right.equalTo(self.textField.mas_right);
                make.centerY.equalTo(self.textField);
        }];
}

- (void)configTimerLabel:(BOOL)force
{

        if ( ![NYUserManager isLastSMSCountTimeCompletion] || force )
        {
                NSInteger time = [NYUserManager getSMSResidueCountTime];
                self.rightView.hidden = YES;
                NSString *text = @"(xxs)重新获取";
                NSRange r = [text rangeOfString:@"xx"];
                self.timerLabel.text = text;
                self.timerLabel.textRange = r;
                [self.timerLabel setCountDownTime:time];
                @weakify(self);
                [self.timerLabel startWithEndingBlock:^(NSTimeInterval countTime) {
                        @strongify(self);
                        self.rightView.hidden = NO;
                        self.textField.text = self.textField.text;
                        self.timerLabel.textRange = NSMakeRange(0, 0);
                        self.timerLabel.text = nil;
                }];
        }
        else
        {
                self.timerLabel.text = nil;
        }
}
- (MZTimerLabel *)timerLabel
{
        if (_timerLabel == nil) {
                _timerLabel = [[MZTimerLabel alloc] initWithTimerType:(MZTimerLabelTypeTimer)];
                _timerLabel.timeFormat = @"ss";
                _timerLabel.timeLabel.textColor = UIColorHex(0xcad1e0);
                _timerLabel.enabled = NO;
                _timerLabel.font = WGSystem13Font;
                _timerLabel.textAlignment = NSTextAlignmentRight;
                _timerLabel.textColor = UIColorHex(0xcad1e0);
                _timerLabel.resetTimerAfterFinish = YES;
        }
        return _timerLabel;
}
- (QMUIButton *)rightView
{
        if (_rightView == nil) {
                _rightView = [[QMUIButton alloc] init];
                _rightView.titleLabel.font = WGSystem13Font;
                _rightView.adjustsTitleTintColorAutomatically = NO;
                _rightView.adjustsButtonWhenHighlighted = NO;
                _rightView.adjustsButtonWhenDisabled = NO;
                [_rightView setTitle:[NSString aPP_GetSMSCode] forState:(UIControlStateDisabled)];
                [_rightView setTitle:[NSString aPP_GetSMSCode] forState:(UIControlStateNormal)];
                [_rightView setTitleColor:UIColorHex(0xe6e6e6) forState:(UIControlStateDisabled)];
                [_rightView setTitleColor:UIColorHex(0x5396F7) forState:(UIControlStateNormal)];
                _rightView.enabled = NO;
        }
        return _rightView;
}
@end
